<template>
  <div class="pieChart">
    <div :id="pieData.pieId" class="echart"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
import colorConst from "@/utils/custom-color.js";
echarts.use([PieChart]);
export default {
  name: "pieChart",
  props: {
    // 饼图数据
    pieData: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      chart: null,
      pieValue: null,
    };
  },
  mounted() {
    setTimeout(() => {
      this.pieCharts(this.pieData);
    }, 100);
  },
  beforeDestroy() {
    this.chartDes();
  },
  watch: {
    pieData: {
      handler(val) {
        if (val) {
          this.chartDes();
          this.pieCharts(this.pieData);
        }
      },
      // deep: true,
      // immediate: true,
    },
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    pieCharts(pieData) {
      let option = {
        color: colorConst.overviewPublicInstitutions,
        // title: {
        //   text: pieData.title,
        //   textStyle: {
        //     fontSize: 20,
        //     fontWeight: 400,
        //   },
        //   // subtext: '副标题',
        //   left: "center",
        //   // top: "5%",
        // },
        tooltip: {
          trigger: "item",
        },
        legend: pieData.legend,
        series: [
          {
            name: pieData.title,
            type: "pie",
            top: "10",
            radius: pieData.radius,
            roseType: pieData.roseType,
            avoidLabelOverlap: false,
            selectedMode: pieData.selectedMode,
            itemStyle: {
              borderRadius: pieData.borderRadius ? pieData.borderRadius : "", // 设置圆角
              borderColor: "#fff",
              borderWidth: 1,
            },
            label: {
              show: false,
              position: pieData.position,
              color: "#1890FF",
              formatter: function (params) {
                return params.value; // 中间显示数据
              },
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "20",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: pieData.dataArr,
          },
        ],
      };
      this.initChart(pieData.pieId, option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
      this.handleClick();
    },
    handleClick() {
      let that = this;
      that.chart.on("click", function (value) {
        that.$emit("clickPie", value);
      });
    },
  },
};
</script>

<style scoped lang="scss">
.pieChart {
  .echart {
    width: 100%;
    height: 220px;
  }
}
</style>
